﻿package
{

    import flash.net.NetConnection;
    import flash.net.ObjectEncoding;
    import flash.events.NetStatusEvent;
	import flash.net.Responder;
    import flash.display.Sprite;
	import flash.events.SyncEvent;
	import flash.net.SharedObject;
	import flash.events.MouseEvent;
	import flash.events.Event;
	 import flash.events.KeyboardEvent;
	  import flash.display.Sprite;
 import flash.display.MovieClip;
 import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
	



    public class Red5FirstClient extends Sprite
    {
		
		
		
var myid:Number = 1;


  private var drawing_so:SharedObject;
  private var nc:NetConnection;
  private var good:Boolean;
  private var isDrawing:Boolean;
  private var penMC:MovieClip;
    private var penMC2:MovieClip;
  private var rpenMC:MovieClip;
    // Mouse Coordinates
    private var startX:int=5;
    private var startY:int=5;
    private var endX:int;
    private var endY:int;

  // Remote mouse coordinates
    private var rstartX:int;
    private var rstartY:int;
    private var rendX:int;
    private var rendY:int;
	var i:int = 1;
	var i2:int = 1;
	var zaman:Timer = new Timer(2500);

	 private var anlikzaman:int;

	 private var so:SharedObject;
	 private var kordinatlar:Array;
	
 public var pathx:Array = new Array();
  public var gec:Array = new Array();
  private var pathobj:Object = new Object();
    private var pathobj2:Object = new Object();
  private var gecicibosrray:Array = new Array();


			// network 
			private var connection:NetConnection;
			private var rso:SharedObject;
			private var responder:Responder;
			
			// logic
			private var dragging:Boolean = false;
			 
			private var player:Array = new Array();
			
			
			 private var penTipSize:uint = 3;
  			private var penColor:Number = 000000;
  private  var txt;
			
			
			public function Red5FirstClient() : void {
				var paint = new Paint();
		addChild(paint);
				   txt = new TextField();
				txt.text="bilgi";
				txt.x=100;
				txt.width=200;
				addChild(txt);
				 zaman.addEventListener(TimerEvent.TIMER, zaman_fonksyion);

   penMC = new MovieClip();
    penMC2 = new MovieClip();
				
				
				 penMC.graphics.lineStyle(penTipSize, penColor, 1.0);
  			
				
				
				// setup connection code
				connection = new NetConnection();
				connection.addEventListener( NetStatusEvent.NET_STATUS , netStatus ); //ben ekledim
				connection.connect("rtmp://localhost/cizanlat");
				connection.client = this;
				// create the remote SharedObject
				rso = SharedObject.getRemote("coordinates", connection.uri);
				rso.addEventListener(SyncEvent.SYNC, onSync);
				rso.connect(connection);
				
				// instantiate the embedded asset
				
				
					
						
	
				addEventListener(Event.ENTER_FRAME,Main);
				
				   paint.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
   paint.addEventListener(MouseEvent.MOUSE_MOVE, drawing);
   paint.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
  paint.addChild(penMC);
    paint.addChild(penMC2);
				
			}
			
			
function zaman_fonksyion(event:TimerEvent):void {
	
	anlikzaman=i;
trace(anlikzaman);


		
	
		
		
var deleted_people:Array = pathx.splice(0,pathx.length);
pathobj={x:deleted_people}


if(deleted_people.length==0){
	trace("Gondermedim!!!!!!!!!!");
	}else{
	rso.setProperty("coordinates", pathobj);
	trace("GONDERDIM"+deleted_people.length);
//	trace("GONDERDIM"+deleted_people);
}

i++;
}




private function startDrawing (e:MouseEvent):void
  {
	   
	//  
	//  trace("sildim"+pathx.length);
   trace("Start drawing");
   isDrawing = true;
 
   startX = mouseX;
   startY = mouseY;
   pathx.push(startX,startY);
   penMC.graphics.moveTo(startX, startY);
   
     zaman.start();
  }



  private function stopDrawing (e:MouseEvent):void
  {
   trace("Stop drawing");
   isDrawing = false;

  var deleted_people2:Array = pathx.splice(0,pathx.length);
pathobj2={x:deleted_people2}

	rso.setProperty("coordinates", pathobj2);
	trace("GONDERDIM"+deleted_people2.length);

	
//	pathobj={x:pathx}
	
	//rso.setProperty("coordinates", pathobj);
	 // trace("gonderdim"+pathx.length);

   
  }
  
  


  private function drawing (e:MouseEvent):void
  {
   if (isDrawing)
   {
 
	
    endX = mouseX;
    endY = mouseY;
	pathx.push(endX,endY);
	
	if(pathx.length>=100)
	{
		//pathobj={x:pathx};
		//rso.setProperty("coordinates", pathobj);
		// trace("parcali adet"+pathx.length);
		// trace(pathx);
		//trace(pathx.length);
		//pathx.splice(0,48);
		//trace(pathx.length);
		 
	}
    penMC.graphics.lineTo(endX, endY);
	
   }
  }


    private function arrayciz(pathx):void
  {
	 
	  
	  penMC.graphics.moveTo(pathx[0], pathx[1]);
	
      for(var i=0;i<pathx.length;i=i+2)
  		 {
			 
	   penMC.graphics.lineTo(pathx[i], pathx[i+1]);

	}
	//penColor=penColor+111111;
	//penTipSize=penTipSize-0.5;
	//penMC.graphics.lineStyle(penTipSize, penColor, 1.0);
	trace(penColor);
  }
  


			private function onSync(event:SyncEvent) : void {
				//trace("event: " + event);
				
				for(var i:Object in event.changeList) {
					var changeObj:Object = event.changeList[i];
					
					switch(changeObj.code) {
						case "success":
							break;
						
						case "change":
							var coordinates = this.rso.data[changeObj.name];
							// zaman2.start();
							//kordinatlar=coordinates.x;
							arrayciz(coordinates.x);
							trace(coordinates.x);
							break;
					}
					
				}
				
			}

			



function Main(event:Event){
	//myid=myid+1;
	//var obj:Object = {x:myid};
	//rso.setProperty("coordinates", obj);
	
	
	
}






			
			 private function netStatus ( event:NetStatusEvent ):void
        {

            trace( event.info.code );
			txt.text=event.info.code;
            if ( event.info.code == "NetConnection.Connect.Rejected" )
            {

           

                trace( event.info.application );

            }
			
			if ( event.info.code == "NetConnection.Connect.Success" )
            {
              trace("olduuu");
			
			    
            }

        }
    }

}